package com.school.base.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.school.base.dao.IClassDao;
import com.school.base.model.Class;
import com.school.common.tags.PageBean;

public class ClassService {
	private static Logger logger = Logger.getLogger(ClassService.class);
	IClassDao classDao;

	public IClassDao getClassDao() {
		return classDao;
	}

	public void setClassDao(IClassDao classDao) {
		this.classDao = classDao;
	}

	public Class getClassById(int id) {
		return classDao.getClassById(id);
	}

	public List<Class> getClassList(Class cls, PageBean pageBean) {
		if (pageBean != null) {
			pageBean = new PageBean();
		}
		pageBean.setTotalRecordCount(classDao.getTotalCount(cls));
		return classDao.getClassList(cls, pageBean.getOffset(), pageBean.getPageSize());
	}

	public void updateClass(Class cls) throws Exception {
		if (cls.getName() == null && cls.getName().isEmpty()) {
			logger.error("The class's name is empty.");
			throw new Exception("班级名称不能为空");
		}
		classDao.updateClass(cls);
	}

	public void deleteClass(int id) {
		classDao.deleteClass(id);
	}

	public void insertClass(Class cls) throws Exception {
		if (cls.getName() == null && cls.getName().isEmpty()) {
			logger.error("The class's name is empty.");
			throw new Exception("班级名称不能为空");
		}
		classDao.insertClass(cls);
	}

	public List<Class> getAllUnexpiredByGrade(String grade) {
		return classDao.getAllUnexpiredByGrade(grade);
	}
}
